﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.IO;
using Tool12306.Models;
using System.Threading;

namespace Tool12306.Windows
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : System.Windows.Window
    {
        private HttpClient _client;
        public LoginModel LoginModel { get; private set; }

        public LoginWindow(HttpClient client)
        {
            InitializeComponent();

            _client = client;


            this.Loaded += new RoutedEventHandler(LoginWindow_Loaded);
        }

        void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            OnGet();
            this.IsEnabled = true;
        }

        private void OnGet()
        {
            LoadVerifyImage();

            _12306Proxy proxy = new _12306Proxy(_client);

            while (LoginModel == null)
            {
                LoginModel = proxy.GetLoginModel();
                Thread.Sleep(200);
            }

            LoginModel.UserName = "ly.jaeho@gmail.com";
            LoginModel.Password = "xk161087264";
            //LoginModel.UserName = "acoolna";
            //LoginModel.Password = "10232588";
            textBox2.Password = LoginModel.Password;

            this.DataContext = LoginModel;
        }

        private void LoadVerifyImage()
        {
            _12306Proxy proxy = new _12306Proxy(_client);

            byte[] data = proxy.GetVerifyImage();
            if (data != null)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = new MemoryStream(data);
                bi.EndInit();
                image1.Source = bi;
            }
            else
            {
                while (data == null)
                {
                    data = proxy.GetVerifyImage();
                    if (data != null)
                    {
                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = new MemoryStream(data);
                        bi.EndInit();
                        image1.Source = bi;
                        Thread.Sleep(200);
                    }
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            LoginModel.Password = textBox2.Password;
            _12306Proxy proxy = new _12306Proxy(_client);
            bool result = proxy.Login(LoginModel);
            while (!result)
            {
                result = proxy.Login(LoginModel);
            }

            DialogResult = true;
            //else
            //{
            //    MessageBox.Show("登录失败!", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            //    this.IsEnabled = false;
            //    LoadVerifyImage();
            //    this.IsEnabled = true;
            //}
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            LoadVerifyImage();
            this.IsEnabled = true;
            //_12306Proxy proxy = new _12306Proxy(_client);

            //byte[] data = proxy.GetVerifyImage();
            //BitmapImage bi = new BitmapImage();
            //bi.BeginInit();
            //bi.StreamSource = new MemoryStream(data);
            //bi.EndInit();
            //image1.Source = bi;
        }
    }
}
